package me.pcqpcq.android.tvlauncher.fragment;

import java.util.Timer;
import java.util.TimerTask;

import me.pcqpcq.android.tvlauncher.BaseFragment;
import me.pcqpcq.android.tvlauncher.R;
import me.pcqpcq.android.tvlauncher.adapter.WheelGroupAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 *
 * 轮播图区域
 * @author pcqpcq
 * @version 1.0.0
 * @since 13-10-21 下午11:44
 */
public class WheelGroupFragment extends BaseFragment {

    private ViewPager mVipImageWheelGroup;

    private Timer mTimer;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_wheel_group, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setBackgroundColor(0xFFFFFA2D);

        initUI(view);
        // 执行轮播
        sendWheelMsg();
    }

    private void initUI(View view) {
        mVipImageWheelGroup = (ViewPager) view.findViewById(R.id.vip_wheel_group);

        //TODO: Test
        mVipImageWheelGroup.setAdapter(new WheelGroupAdapter(getActivity()));
    }

    /**
     * 发送轮播消息
     */
    public void sendWheelMsg() {
        if(null == mVipImageWheelGroup) {
            return;
        }
        if(null != mTimer) {
            return;
        }
        mTimer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                if (getActivity() == null) {
                    return ;
                }
                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mVipImageWheelGroup.setCurrentItem(mVipImageWheelGroup.getCurrentItem() + 1);
                    }
                });
            }
        };
        mTimer.schedule(task, 2000, 4000);
    }
}
